﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.DataVisualization;
using System.Web.UI.DataVisualization.Charting;
using System.Collections.Generic;
using System.Text;

using CE.Global;
using CE.Database;

public partial class iFrame : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (Session[General.UserRole] != null && Session[General.UserRole].ToString() == "201")
        {
            Response.Redirect("~/carinsurance/PendingApplication.aspx");
        }

        if (!Page.IsPostBack)
        {
            DrawChart();
        }
    }

    public void DrawChart()
    {
        StringBuilder strScript = new StringBuilder();
        try
        {
            Int32 _RankBy = 0;
            MyDatabase _db = new MyDatabase();
            string _spName = "";
            DataSet _dsAmount = new DataSet();

            if (Session[General.UserRole] != null && Session[General.UserRole].ToString() == "100")
            {
                Int64 intLoginUserId = 0;
                if (Session[General.UserId] != null)
                {
                    if (!Int64.TryParse(Session[General.UserId].ToString(), out intLoginUserId))
                    {
                        intLoginUserId = 0;
                    }
                }
                _spName = "sprocCXCRMAgentSalesRankSelectedItemList";
                SortedList _paraList = new SortedList();
                _paraList.Add("@LoginAgentId", intLoginUserId);
                //_paraList.Add("@LoginAgentId", "59141");
                _dsAmount = _db.GetDataSet(_spName, _paraList);

                #region Chart Values
                DataTable _dtAgentSales = new DataTable();
                DataTable _dtAgentChart = new DataTable();
                if (_dsAmount != null && _dsAmount.Tables.Count > 0)
                {
                    if (_dsAmount.Tables[0].Rows.Count > 0)
                    {
                        _dtAgentSales = _dsAmount.Tables[0];
                        _RankBy = _dsAmount.Tables[0].Rows.Count;
                    }
                    if (_dsAmount.Tables.Count > 3 && _dsAmount.Tables[3].Rows.Count > 0)
                    {
                        _dtAgentChart = _dsAmount.Tables[3];
                    }
                }
                //if (Session[General.IsRenewalAgent] != null && Session[General.IsRenewalAgent].ToString() == "1")
                //{
                //    if (_dsAmount != null && _dsAmount.Tables.Count > 0)
                //    {
                //        if (_dsAmount.Tables.Count > 1 && _dsAmount.Tables[1].Rows.Count > 0)
                //        {
                //            _dtAgentSales = _dsAmount.Tables[1];
                //            _RankBy = _dsAmount.Tables[1].Rows.Count;
                //        }
                //    }
                //}
                //else
                //{
                //    if (_dsAmount != null && _dsAmount.Tables.Count > 0)
                //    {
                //        if (_dsAmount.Tables.Count > 0 && _dsAmount.Tables[0].Rows.Count > 0)
                //        {
                //            _dtAgentSales = _dsAmount.Tables[0];
                //            _RankBy = _dsAmount.Tables[0].Rows.Count;
                //        }
                //    }
                //}
                #endregion

                #region Chart
                if (_dtAgentChart != null && _dtAgentChart.Rows.Count > 0)
                {
                    string _Title = Resources.General.AgentSales;// AgentsSalesRanking;                    
                    strScript.Append(@"<script type='text/javascript'>");
                    strScript.Append("window.onload = function () {");
                    
                    #region Chart New Sales
                    strScript.Append(" var chart = new CanvasJS.Chart('chartContainer', {");
                    strScript.Append("title:{");
                    strScript.Append("text:'" + _Title + "'");
                    strScript.Append("},");
                    strScript.Append("animationEnabled: true,");
                    strScript.Append("axisX:{");
                    strScript.Append("interval: 1,");
                    strScript.Append("labelFontSize: 12,");
                    strScript.Append("lineThickness: 0");
                    strScript.Append("},");
                    strScript.Append("axisY2:{");
                    strScript.Append("valueFormatString: ' 0',");
                    strScript.Append("lineThickness: 0  ");
                    strScript.Append("},");
                    strScript.Append("toolTip: {");
                    strScript.Append("shared: true");
                    strScript.Append("},");
                    strScript.Append("legend:{");
                    strScript.Append("verticalAlign: 'top',");
                    strScript.Append("horizontalAlign: 'center'");
                    strScript.Append("},");
                    strScript.Append("data: [");
                    strScript.Append("{");
                    strScript.Append("type: 'stackedBar',");
                    strScript.Append("showInLegend: true,");
                    strScript.Append("name: '" + Resources.General.Collection + "',");
                    strScript.Append("axisYType: 'secondary',");
                    strScript.Append("color: '#85b402',");
                    strScript.Append("dataPoints: [");
                    foreach (DataRow row in _dtAgentChart.Rows)
                    {
                        string _values = "{y: " + row["IndCollectedAmount"].ToString() + ", label: '" + row["NameofMonth"].ToString() + "'},";
                        strScript.Append(_values);
                    }
                    strScript.Append("]");
                    strScript.Append(" },");
                    strScript.Append("{ ");
                    strScript.Append("type: 'stackedBar',");
                    strScript.Append("showInLegend: true,");
                    strScript.Append("name: '" + Resources.General.TargetBalance + "',");
                    strScript.Append("axisYType: 'secondary',");
                    strScript.Append("color: '#cccccc',");
                    strScript.Append("dataPoints: [");
                    foreach (DataRow row in _dtAgentChart.Rows)
                    {
                        string _values = "{y: " + row["BalanceAmount"].ToString() + ", label: '" + row["NameofMonth"].ToString() + "'},";
                        strScript.Append(_values);
                    }
                    strScript.Append("]");
                    strScript.Append("},");
                    strScript.Append("{");
                    strScript.Append("type: 'stackedBar',");
                    strScript.Append("showInLegend: true,");
                    strScript.Append("name: '" + Resources.General.OverTraget + "',");
                    strScript.Append("axisYType: 'secondary',");
                    strScript.Append("color: '#f8d347',");
                    strScript.Append("dataPoints: [");
                    foreach (DataRow row in _dtAgentChart.Rows)
                    {
                        string _values = "{y: " + row["OvertheTarget"].ToString() + ", label: '" + row["NameofMonth"].ToString() + "'},";
                        strScript.Append(_values);
                    }
                    strScript.Append("]");
                    strScript.Append("}");
                    strScript.Append("]");
                    strScript.Append("});");
                    strScript.Append(" chart.render();");
                    #endregion

                    strScript.Append("}");
                    strScript.Append("</script>");
                }
                ltScripts.Text = strScript.ToString();
                #endregion
            }
            else if (Session[General.UserRole] != null && Session[General.UserRole].ToString() != "100")
            {
                _spName = "sprocCXCRMAgentSalesTargetSelectedItemList";
                SortedList _paraList = new SortedList();
                _paraList.Add("@LoginType", "2");
                _dsAmount = _db.GetDataSet(_spName, _paraList);

                #region Chart Values
                DataTable _dtAgentSales = new DataTable();
                if (_dsAmount != null && _dsAmount.Tables.Count > 2)
                {
                    if (_dsAmount.Tables[2].Rows.Count > 0)
                    {
                        _dtAgentSales = _dsAmount.Tables[2];
                        _RankBy = _dsAmount.Tables[2].Rows.Count;
                    }
                }
                #endregion

                #region Chart
                if (_dtAgentSales != null && _dtAgentSales.Rows.Count > 0)
                {
                    string _Title = Resources.General.AgentSales;// AgentsSalesRanking;
                    strScript.Append(@"<script type='text/javascript'>");
                    strScript.Append("window.onload = function() {");

                    #region Chart Agent Rank
                    strScript.Append(" var chart = new CanvasJS.Chart('chartContainer', {");
                    strScript.Append("title:{");
                    strScript.Append("text:'" + _Title + "'");
                    strScript.Append("},");
                    strScript.Append("animationEnabled: true,");
                    strScript.Append("axisX:{");
                    strScript.Append("interval: 1,");
                    strScript.Append("labelFontSize: 12,");
                    strScript.Append("lineThickness: 0");
                    strScript.Append("},");
                    strScript.Append("axisY2:{");
                    strScript.Append("valueFormatString: ' 0',");
                    strScript.Append("lineThickness: 0  ");
                    strScript.Append("},");
                    strScript.Append("toolTip: {");
                    strScript.Append("shared: true");
                    strScript.Append("},");
                    strScript.Append("legend:{");
                    strScript.Append("verticalAlign: 'top',");
                    strScript.Append("horizontalAlign: 'center'");
                    strScript.Append("},");
                    strScript.Append("data: [");
                    strScript.Append("{");
                    strScript.Append("type: 'stackedBar',");
                    strScript.Append("showInLegend: true,");
                    strScript.Append("name: '" + Resources.General.Collection + "',");
                    strScript.Append("axisYType: 'secondary',");
                    strScript.Append("color: '#85b402',");
                    strScript.Append("dataPoints: [");
                    foreach (DataRow row in _dtAgentSales.Rows)
                    {
                        string _values = "{y: " + row["CollectedAmount"].ToString() + ", label: '" + row["AgentCodeId"].ToString() + "'},";
                        strScript.Append(_values);
                    }
                    strScript.Append("]");
                    strScript.Append(" },");
                    strScript.Append("{ ");
                    strScript.Append("type: 'stackedBar',");
                    strScript.Append("showInLegend: true,");
                    strScript.Append("name: '" + Resources.General.TargetBalance + "',");
                    strScript.Append("axisYType: 'secondary',");
                    strScript.Append("color: '#cccccc',");
                    strScript.Append("dataPoints: [");
                    foreach (DataRow row in _dtAgentSales.Rows)
                    {
                        string _values = "{y: " + row["BalanceAmount"].ToString() + ", label: '" + row["AgentCodeId"].ToString() + "'},";
                        strScript.Append(_values);
                    }
                    strScript.Append("]");
                    strScript.Append("},");
                    strScript.Append("{");
                    strScript.Append("type: 'stackedBar',");
                    strScript.Append("showInLegend: true,");
                    strScript.Append("name: '" + Resources.General.OverTraget + "',");
                    strScript.Append("axisYType: 'secondary',");
                    strScript.Append("color: '#f8d347',");
                    strScript.Append("dataPoints: [");
                    foreach (DataRow row in _dtAgentSales.Rows)
                    {
                        string _values = "{y: " + row["OvertheTarget"].ToString() + ", label: '" + row["AgentCodeId"].ToString() + "'},";
                        strScript.Append(_values);
                    }
                    strScript.Append("]");
                    strScript.Append("}");
                    strScript.Append("]");
                    strScript.Append("});");
                    strScript.Append(" chart.render();");
                    #endregion

                    strScript.Append("}");
                    strScript.Append("</script>");
                }
                ltScripts.Text = strScript.ToString();
                #endregion
            }

        }
        catch (Exception ex)
        {
        }
        finally
        {

        }
    }

    #region Not Using

    //public void DrawChart_05Mar()
    //{
    //    StringBuilder strScript = new StringBuilder();
    //    try
    //    {
    //        Int32 _RankBy = 0;
    //        MyDatabase _db = new MyDatabase("GGR_DBConnectionString_TEST");
    //        string _spName = "";
    //        DataSet _dsAmount = new DataSet();

    //        if (Session[General.UserRole] != null && Session[General.UserRole].ToString() != "100")
    //        {
    //            Int64 intLoginUserId = 0;
    //            if (Session[General.UserRole] != null)
    //            {
    //                if (!Int64.TryParse(Session[General.UserRole].ToString(), out intLoginUserId))
    //                {
    //                    intLoginUserId = 0;
    //                }
    //            }
    //            _spName = "sprocCXCRMAgentSalesRankSelectedItemList";
    //            SortedList _paraList = new SortedList();
    //            _paraList.Add("@LoginAgentId", intLoginUserId);
    //            //_paraList.Add("@LoginAgentId", "59141");
    //            _dsAmount = _db.GetDataSet(_spName, _paraList);

    //            #region Chart Values
    //            DataTable _dtAgentSales = new DataTable();
    //            DataTable _dtAgentChart = new DataTable();
    //            if (_dsAmount != null && _dsAmount.Tables.Count > 0)
    //            {
    //                if (_dsAmount.Tables[0].Rows.Count > 0)
    //                {
    //                    _dtAgentSales = _dsAmount.Tables[0];
    //                    _RankBy = _dsAmount.Tables[0].Rows.Count;
    //                }
    //                if (_dsAmount.Tables.Count > 3 && _dsAmount.Tables[3].Rows.Count > 0)
    //                {
    //                    _dtAgentChart = _dsAmount.Tables[3];
    //                }
    //            }
    //            //if (Session[General.IsRenewalAgent] != null && Session[General.IsRenewalAgent].ToString() == "1")
    //            //{
    //            //    if (_dsAmount != null && _dsAmount.Tables.Count > 0)
    //            //    {
    //            //        if (_dsAmount.Tables.Count > 1 && _dsAmount.Tables[1].Rows.Count > 0)
    //            //        {
    //            //            _dtAgentSales = _dsAmount.Tables[1];
    //            //            _RankBy = _dsAmount.Tables[1].Rows.Count;
    //            //        }
    //            //    }
    //            //}
    //            //else
    //            //{
    //            //    if (_dsAmount != null && _dsAmount.Tables.Count > 0)
    //            //    {
    //            //        if (_dsAmount.Tables.Count > 0 && _dsAmount.Tables[0].Rows.Count > 0)
    //            //        {
    //            //            _dtAgentSales = _dsAmount.Tables[0];
    //            //            _RankBy = _dsAmount.Tables[0].Rows.Count;
    //            //        }
    //            //    }
    //            //}
    //            #endregion

    //            #region Chart
    //            if (_dtAgentChart != null && _dtAgentChart.Rows.Count > 0)
    //            {
    //                string _Title = Resources.General.AgentsSalesRanking;
    //                //strScript.Append(@"<script type='text/javascript'>");
    //                //strScript.Append("window.onload = function() {");

    //                //strScript.Append("var options = {");
    //                //strScript.Append("title: {");
    //                //strScript.Append("text: '" + _Title + "'");
    //                //strScript.Append("},");
    //                //strScript.Append("animationEnabled: true,");
    //                //strScript.Append("data: [");
    //                //strScript.Append("{");
    //                //strScript.Append("type: 'line',");
    //                //strScript.Append("dataPoints: [");

    //                //foreach (DataRow row in _dtAgentChart.Rows)
    //                //{
    //                //    string _values = "{ x: " + row["MonthId"].ToString() + ", y: " + row["CollectedAmount"].ToString() + " },";
    //                //    strScript.Append(_values);
    //                //}
    //                //strScript.Append("]");
    //                //strScript.Append("}");
    //                //strScript.Append("]");
    //                //strScript.Append("};");

    //                //strScript.Append("$('#chartContainer').CanvasJSChart(options);");

    //                //strScript.Append("}");
    //                //strScript.Append("</script>");


    //                strScript.Append(@"<script type='text/javascript'>");
    //                strScript.Append("window.onload = function () {");
    //                strScript.Append(" var chart = new CanvasJS.Chart('chartContainer', {");
    //                strScript.Append("title:{");
    //                strScript.Append("text: '" + _Title + "' ");             
    //                strScript.Append("},");
    //                strScript.Append("data: [      ");        
    //                strScript.Append("{");
    //                strScript.Append("type: 'column',");
    //                strScript.Append("dataPoints: [");
    //                int i = 0;
    //                foreach (DataRow row in _dtAgentChart.Rows)
    //                {
    //                    i += 1;
    //                    if (i < _dtAgentChart.Rows.Count)
    //                    {
    //                        strScript.Append("{ label: '" + row["NameofMonth"].ToString() + "', y: " + row["CollectedAmount"].ToString() + " },");
    //                    }
    //                    else
    //                    {
    //                        strScript.Append("{ label: '" + row["NameofMonth"].ToString() + "', y: " + row["CollectedAmount"].ToString() + " }");
    //                    }
    //                }
    //                strScript.Append("]");
    //                strScript.Append("}");
    //                strScript.Append("],");
    //                strScript.Append("theme: 'theme1'");
    //                strScript.Append("});");
    //                strScript.Append("chart.render();");
    //                strScript.Append("}");
    //                strScript.Append("</script>");
    //            }
    //            ltScripts.Text = strScript.ToString();
    //            #endregion
    //        }
    //        else if (Session[General.UserRole] != null && Session[General.UserRole].ToString() != "100")
    //        {
    //            _spName = "sprocCXCRMAgentSalesTargetSelectedItemList";
    //            SortedList _paraList = new SortedList();
    //            _paraList.Add("@LoginType", "2");
    //            _dsAmount = _db.GetDataSet(_spName, _paraList);

    //            #region Chart Values
    //            DataTable _dtAgentSales = new DataTable();
    //            if (_dsAmount != null && _dsAmount.Tables.Count > 2)
    //            {
    //                if (_dsAmount.Tables[2].Rows.Count > 0)
    //                {
    //                    _dtAgentSales = _dsAmount.Tables[2];
    //                    _RankBy = _dsAmount.Tables[2].Rows.Count;
    //                }
    //            }
    //            #endregion

    //            #region Chart
    //            if (_dtAgentSales != null && _dtAgentSales.Rows.Count > 0)
    //            {
    //                string _Title = Resources.General.AgentsSalesRanking;
    //                strScript.Append(@"<script type='text/javascript'>");
    //                strScript.Append("window.onload = function() {");

    //                strScript.Append("var options = {");
    //                strScript.Append("title: {");
    //                strScript.Append("text: '" + _Title + "'");
    //                strScript.Append("},");
    //                strScript.Append("animationEnabled: true,");
    //                strScript.Append("data: [");
    //                strScript.Append("{");
    //                strScript.Append("type: 'column',");
    //                strScript.Append("dataPoints: [");

    //                foreach (DataRow row in _dtAgentSales.Rows)
    //                {
    //                    //if (row["IndividualAmount"].ToString() != "0")
    //                    //{
    //                    string _values = "{ x: " + row["AgentRank"].ToString() + ", y: " + row["IndividualAmount"].ToString() + " },";
    //                    strScript.Append(_values);
    //                    //}
    //                }
    //                strScript.Append("]");
    //                strScript.Append("}");
    //                strScript.Append("]");
    //                strScript.Append("};");

    //                strScript.Append("$('#chartContainer').CanvasJSChart(options);");

    //                strScript.Append("}");
    //                strScript.Append("</script>");
    //            }
    //            ltScripts.Text = strScript.ToString();
    //            #endregion
    //        }

    //    }
    //    catch (Exception ex)
    //    {
    //    }
    //    finally
    //    {

    //    }
    //}

    //public void DrawChartOld()
    //{
    //    StringBuilder strScript = new StringBuilder();
    //    try
    //    {
    //        Int32 _RankBy = 0;
    //        MyDatabase _db = new MyDatabase("GGR_DBConnectionString_TEST");
    //        //string _spName = "sprocCXCRMAgentSalesTargetSelectedItemList";
    //        string _spName = "sprocCXCRMAgentSalesRankSelectedItemList";
    //        DataSet _dsAmount = new DataSet();
    //        if (Session[General.UserRole] != null)
    //        //if (Session[General.UserRole] != null && Session[General.UserRole].ToString() == "100")
    //        {
    //            SortedList _paraList = new SortedList();
    //            //_paraList.Add("@LoginType", "2");
    //            _paraList.Add("@LoginAgentId", "59141");
    //            _dsAmount = _db.GetDataSet(_spName, _paraList);
    //        }

    //        #region Chart Values
    //        DataTable _dtAgentSales = new DataTable();
    //        if (Session[General.IsRenewalAgent] != null && Session[General.IsRenewalAgent].ToString() == "1")
    //        {
    //            if (_dsAmount != null && _dsAmount.Tables.Count > 0)
    //            {
    //                if (_dsAmount.Tables.Count > 1 && _dsAmount.Tables[1].Rows.Count > 0)
    //                {
    //                    _dtAgentSales = _dsAmount.Tables[1];
    //                    _RankBy = _dsAmount.Tables[1].Rows.Count;
    //                }
    //            }
    //        }
    //        else
    //        {
    //            if (_dsAmount != null && _dsAmount.Tables.Count > 0)
    //            {
    //                if (_dsAmount.Tables.Count > 0 && _dsAmount.Tables[0].Rows.Count > 0)
    //                {
    //                    _dtAgentSales = _dsAmount.Tables[0];
    //                    _RankBy = _dsAmount.Tables[0].Rows.Count;
    //                }
    //            }
    //        }
    //        #endregion

    //        #region Chart
    //        if (_dtAgentSales != null && _dtAgentSales.Rows.Count > 0)
    //        {
    //            string _Title = Resources.General.AgentsSalesRanking;
    //            strScript.Append(@"<script type='text/javascript'>");
    //            strScript.Append("window.onload = function() {");

    //            strScript.Append("var options = {");
    //            strScript.Append("title: {");
    //            strScript.Append("text: '" + _Title + "'");
    //            strScript.Append("},");
    //            strScript.Append("animationEnabled: true,");
    //            strScript.Append("data: [");
    //            strScript.Append("{");
    //            strScript.Append("type: 'column',");
    //            strScript.Append("dataPoints: [");

    //            foreach (DataRow row in _dtAgentSales.Rows)
    //            {
    //                //if (row["IndividualAmount"].ToString() != "0")
    //                //{
    //                float _IndCollection = 0.0f;
    //                if (!float.TryParse(row["IndividualAmount"].ToString(), out _IndCollection))
    //                {
    //                    _IndCollection = 0.0f;
    //                }                    
    //                string _values = "{ x: " + row["AgentRank"].ToString() + ", y: " + row["IndividualAmount"].ToString() + " },";
    //                strScript.Append(_values);
    //                //}
    //            }
    //            strScript.Append("]");
    //            strScript.Append("}");
    //            strScript.Append("]");
    //            strScript.Append("};");

    //            strScript.Append("$('#chartContainer').CanvasJSChart(options);");

    //            strScript.Append("}");
    //            strScript.Append("</script>");
    //        }
    //        ltScripts.Text = strScript.ToString();
    //        #endregion

    //    }
    //    catch (Exception ex)
    //    {
    //    }
    //    finally
    //    {

    //    }
    //}

    #endregion

}
